Imports System.Data
Imports System.Data.SqlClient
Imports Conexao_liion


Partial Class CFOP
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'If Not Session("Logged") Then
        'Response.Redirect("Login.aspx")
        'End If

        Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache)

        ' Header
        lblModulo.Text = "Administrador"
        lblModuloItem.Text = "Cadastro de CFOP"

        btnIncluir.Enabled = False
        btnAlterar.Enabled = False
        btnExcluir.Enabled = False
        btnDadosFiscais.Enabled = False

        If (CarregaGVW() = True) Then
            btnIncluir.Enabled = True
        End If

    End Sub


    Private Function CarregaGVW() As Boolean

        CarregaGVW = False

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String = "SELECT CodCFOP, DscCFOP" & _
                     " FROM CFOP" & _
                     " ORDER BY CodCFOP;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                gvwData.DataSource = mydatatable
                gvwData.DataBind()

                CarregaGVW = True

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using
    End Function

    Protected Sub gvwData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwData.PageIndexChanging

        Try
            gvwData.PageIndex = e.NewPageIndex
            gvwData.DataBind()
        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Protected Sub gvwData_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwData.SelectedIndexChanged

        'If Not Page.IsPostBack Then
        btnAlterar.Enabled = True
        btnExcluir.Enabled = True
        btnDadosFiscais.Enabled = True
        'End If

    End Sub



    Protected Sub btnIncluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncluir.Click

        Session.Add("strOperacao", "I") ' Incluir

        Session.Add("strCodCFOP", "")
        Session.Add("strDscCFOP", "")

        Response.Redirect("CFOPData.aspx")

    End Sub

    Protected Sub btnAlterar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlterar.Click

        Session.Add("strOperacao", "A") ' Alterar

        Session.Add("strCodCFOP", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strDscCFOP", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))

        Response.Redirect("CFOPData.aspx")

    End Sub

    Protected Sub btnExcluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcluir.Click

        Session.Add("strOperacao", "E") ' Excluir

        Session.Add("strCodCFOP", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strDscCFOP", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))

        Response.Redirect("CFOPData.aspx")

    End Sub

    Protected Sub btnVoltar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltar.Click

        Response.Redirect("Default.aspx")

    End Sub

End Class
